import { request } from '../http'

/** 获取设备类型列表（分页） */
export function fetchDeviceTypeList(params: { pageNo?: number, pageSize?: number, page?: number }) {
  return request.Get<Service.ResponseResult<Api.DeviceType.PageResponse>>('/api/device-type/list', { params })
}

/** 获取所有设备类型 */
export function fetchAllDeviceTypes() {
  return request.Get<Service.ResponseResult<Api.DeviceType.Info[]>>('/api/device-types')
}

/** 获取单个设备类型 */
export function fetchDeviceType(name: string) {
  return request.Get<Service.ResponseResult<Api.DeviceType.Info>>(`/api/device-type/${name}`)
}

/** 添加设备类型 */
export function addDeviceType(data: Api.DeviceType.AddRequest) {
  return request.Post<Service.ResponseResult<null>>('/api/device-type', data)
}

/** 更新设备类型 */
export function updateDeviceType(data: Api.DeviceType.UpdateRequest) {
  return request.Put<Service.ResponseResult<null>>('/api/device-type', data)
}

/** 删除设备类型 */
export function deleteDeviceType(name: string) {
  return request.Delete<Service.ResponseResult<null>>(`/api/device-type/${name}`)
}

/** 获取设备类型的所有属性 */
export function fetchDeviceProperties(name: string) {
  return request.Get<Service.ResponseResult<Api.DeviceType.Property[]>>(`/api/device-property/${name}`)
}

/** 添加设备属性 */
export function addDeviceProperty(name: string, data: Api.DeviceType.Property) {
  return request.Post<Service.ResponseResult<null>>(`/api/device-property/${name}`, data)
}

/** 更新设备属性 */
export function updateDeviceProperty(name: string, propertyId: number, data: Api.DeviceType.Property) {
  return request.Put<Service.ResponseResult<null>>(`/api/device-property/${name}/${propertyId}`, data)
}

/** 删除设备属性 */
export function deleteDeviceProperty(name: string, propertyId: number) {
  return request.Delete<Service.ResponseResult<null>>(`/api/device-property/${name}/${propertyId}`)
}

/** 上传协议包（zip） */
export function uploadProtocol(name: string, file: File) {
  const formData = new FormData()
  formData.append('FileName', file)
  return request.Post<Service.ResponseResult<Api.DeviceType.Info>>(`/api/device-type/upload/${name}`, formData)
}

// ========== Lua 文件管理 ==========

/** 获取协议的所有 Lua 文件 */
export function fetchLuaFiles(driver: string) {
  return request.Get<Service.ResponseResult<Api.DeviceType.LuaFiles>>(`/api/device-type/lua/${driver}`)
}

/** 获取单个 Lua 文件内容 */
export function fetchLuaFile(driver: string, filename: string) {
  return request.Get<Service.ResponseResult<Api.DeviceType.LuaFileContent>>(`/api/device-type/lua/${driver}/${filename}`)
}

/** 保存 Lua 文件 */
export function saveLuaFile(driver: string, filename: string, content: string) {
  return request.Post<Service.ResponseResult<null>>(`/api/device-type/lua/${driver}/${filename}`, { content })
}

/** 获取协议使用情况 */
export function fetchDriverUsage(driver: string) {
  return request.Get<Service.ResponseResult<Api.DeviceType.DriverUsage>>(`/api/device-type/driver-usage/${driver}`)
}

/** 列出所有可用的协议 */
export function fetchAllDrivers() {
  return request.Get<Service.ResponseResult<string[]>>('/api/device-type/drivers')
}

/** 测试协议解析 */
export function testProtocolParsing(driver: string, data: { messageData: string, format?: 'hex' | 'base64' }) {
  return request.Post<Service.ResponseResult<any>>(`/api/device-type/test-protocol/${driver}`, data)
}
